import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import layout from '../layout/layout'

Vue.use(VueRouter)

// 解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) { return originalPush.call(this, location, onResolve, onReject) }
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject) { return originalReplace.call(this, location, onResolve, onReject) }
  return originalReplace.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "Login" */ '../views/login/login')
  },
  {
    path: '/articleDetail/:id',
    name: 'ArticleDetail',
    component: () => import(/* webpackChunkName: "ArticleDetail" */ '../views/news/articleDetail/articleDetail')
  },
  {
    path: '/association',
    name: 'Association',
    component: () => import(/* webpackChunkName: "Association" */ '../views/association/Association')
  },
  {
    path: '/serve',
    name: 'Serve',
    component: () => import(/* webpackChunkName: "Serve" */ '../views/serve/Serve')
  },
  {
    path: '/demo',
    name: 'Demo',
    component: () => import(/* webpackChunkName: "Demo" */ '../views/demo/Demo')
  },
  {
    path: '/mock',
    name: 'Mock',
    component: () => import(/* webpackChunkName: "Mock" */ '../views/demo/Mock')
  },
  {
    path: '/grouplist',
    name: 'Grouplist',
    component: () => import(/* webpackChunkName: "Grouplist" */ '../views/grouplist/Grouplist')
  },
  {
    path: '/',
    name: 'Layout',
    component: layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import(/* webpackChunkName: "Login" */ '../views/home/Home'),
        meta: {
          keepAlive: true
        }
      },
      {
        path: 'news',
        name: 'News',
        component: () => import(/* webpackChunkName: "News" */ '../views/news/News')
      },
      {
        path: 'myactivity',
        name: 'Myactivity',
        component: () => import(/* webpackChunkName: "Myactivity" */ '../views/myactivity/Myactivity')
      },
      {
        path: 'club',
        name: 'Club',
        component: () => import(/* webpackChunkName: "Club" */ '../views/club/Club')
      },
      {
        path: 'my',
        name: 'My',
        component: () => import(/* webpackChunkName: "My" */ '../views/my/My')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: to.meta.savedPosition || 0 }
    }
  }
})

export default router
